package com.yc.cloud.business;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.cloud.config.LogCacheConfig;
import com.yc.cloud.log.entity.DataLog;
import com.yc.cloud.log.service.DataLogService;
import com.yc.cloud.model.find.FindDataLogPage;
import com.yc.cloud.model.save.SaveDataLog;
import com.yc.cloud.model.update.UpdateDataLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.yc.cloud.common.constant.BaseConstant.NINETY_NINE;

/**
 * @author 杨智杰
 * @date 2021/8/11 11:28
 */
@Slf4j
@Service
public class LoggerBusiness {

    @Resource
    private DataLogService dataLogService;

    /**
     * 新增
     *
     * @return
     */
    public Boolean save(SaveDataLog param) {
        DataLog dataLog = Convert.convert(DataLog.class, param);
        return dataLogService.save(dataLog.setCreateTime(DateUtil.now()));
    }

    /**
     * 修改
     *
     * @param param
     * @return
     */
    public Boolean update(UpdateDataLog param) {
        return dataLogService.updateById(Convert.convert(DataLog.class, param));
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    public IPage<DataLog> page(FindDataLogPage param) {
        return dataLogService.page(new Page<>(param.getCurrent(), param.getSize()),
                new QueryWrapper<DataLog>().lambda()
                        .eq(ObjectUtil.isNotNull(param.getId()), DataLog::getId, param.getId())
                        .like(StrUtil.isNotBlank(param.getMethod()), DataLog::getMethod, param.getMethod())
                        .like(StrUtil.isNotBlank(param.getHeaders()), DataLog::getHeaders, param.getHeaders())
                        .like(StrUtil.isNotBlank(param.getRequestBody()), DataLog::getRequestBody, param.getRequestBody())
                        .like(StrUtil.isNotBlank(param.getRequestURI()), DataLog::getRequestURI, param.getRequestURI())
                        .or(StrUtil.isNotBlank(param.getLoginName()), i ->
                                i.like(DataLog::getHeaders, StrUtil.DOT + SecureUtil.md5(param.getLoginName()) + StrUtil.DOT)
                                        .or().like(DataLog::getRequestBody, "\"loginName\": \"" + param.getLoginName() + "\""))
                        .ge(StrUtil.isNotBlank(param.getBeginTime()), DataLog::getCreateTime, param.getBeginTime())
                        .le(StrUtil.isNotBlank(param.getEndTime()), DataLog::getCreateTime, param.getEndTime())
                        .orderByDesc(DataLog::getCreateTime)
        );
    }

    /**
     * 删除
     *
     * @param param
     * @return
     */
    public Boolean delete(List<Long> param) {
        return dataLogService.removeByIds(param);
    }

    /**
     * 缓存添加日志
     *
     * @param param
     * @return
     */
    public void setCaChe(SaveDataLog param) {
        LogCacheConfig.logCache.put(SecureUtil.md5(RandomUtil.randomString(NINETY_NINE) + System.currentTimeMillis()), param);
    }
}
